package example;

import com.ibm.tspaces.Field;
import com.ibm.tspaces.Tuple;
import com.ibm.tspaces.TupleSpace;
import com.ibm.tspaces.TupleSpaceException;

public class TSpacesExample {
  public static void main(String[] args) {
    String host = "localhost";
    TupleSpace ts;
    try {
      ts = new TupleSpace("Example1", host);

      // builds a new tuple and writes it to the tuple space.
      Tuple t1 = new Tuple("Key1", "Data1");
      ts.write(t1);

      // builds a new tuple template and tries to read it from the
      // tuple space. The template has two fields, both strings, one
      // with a defined value, "Key1", and the other one undefined.
      Tuple template = new Tuple("Key1", new Field(String.class));
      Tuple tuple = ts.read(template);

      // get and print the first field of the tuple.
      String data = (String) tuple.getField(1).getValue();
      System.out.println(data);

    } catch (TupleSpaceException e) {
      e.printStackTrace();
    }
  }
}
